package com.lmqb.controller;

import com.lmqb.bean.JCollection;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.SysUser;
import com.lmqb.entry.MenuTitle;
import com.lmqb.page.*;
import com.lmqb.service.AccountService;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.RepayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AccountController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private RepayService repayService;
	
	//代理商奖励惩罚
	@RequestMapping(value="agentAccount.html",method=RequestMethod.GET)
	public String toAgentAccount(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,AgentAccountPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(AgentAccountPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			AgentAccountPager userBankPager=accountService.selectAgentAccount(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("还款管理");
			menuTitle.setMenu("代理商奖罚");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "agentAccountList";
	}
	
	//现金账户
	@RequestMapping(value="account.html",method=RequestMethod.GET)
	public String toAccount(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,AccountPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(AccountPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			AccountPager userBankPager=accountService.selectAccountPager(pagerModel);
			request.setAttribute(pager, userBankPager);
//			MenuTitle menuTitle=new MenuTitle();
//			menuTitle.setMenu("代理商奖罚");
//			menuTitle.setParent("还款管理");
//			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "accountList";
	}
	
	//提现日志
	@RequestMapping(value="withdraw.html",method=RequestMethod.GET)
	public String toWithdraw(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,WithdrawPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(WithdrawPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			WithdrawPager userBankPager=accountService.selectWithdrawPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("推广管理");
			menuTitle.setMenu("提现明细");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "withdrawList";
	}
	
	//现金日志明细
	@RequestMapping(value="accountLog.html",method=RequestMethod.GET)
	public String toAccountLog(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,AccountLogPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(AccountLogPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			AccountLogPager userBankPager=accountService.selectAccountLogPager(pagerModel);
			request.setAttribute(pager, userBankPager);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "accountLogList";
	}
	
	//公司所有出账明细
	@RequestMapping(value="companyAccount.html",method=RequestMethod.GET)
	public String toAccountLog(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CompanyAccountPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(CompanyAccountPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CompanyAccountPager userBankPager=accountService.selectCompanyAccountPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("推广管理");
			menuTitle.setMenu("出账明细");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "companyAccountList";
	}
	
	//公司所有进账明细
	@RequestMapping(value="companyExtra.html",method=RequestMethod.GET)
	public String toCompanyExtra(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CompanyExtraPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(CompanyExtraPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CompanyExtraPager userBankPager=accountService.selectCompanyExtraPager(pagerModel);
			request.setAttribute(pager, userBankPager);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "companyExtraList";
	}

	//订单号查询催收订单
	@RequestMapping(value = "Cuishou_repayno.html",method = RequestMethod.GET)
	public String Cuishou_repayno(HttpSession session,HttpServletRequest request,JCollection jCollection){
		try{
				SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
				if(sysUser==null){
				return "login";
			}
				jCollection=accountService.selectCuishou_repayno(jCollection);
				request.setAttribute("jCollection",jCollection);
			}catch(Exception e){
					e.printStackTrace();
			}
           return  "updateCuishou";
	}
	//根据订单号修改
	@RequestMapping(value = "updqteCuishou_repayno",method = RequestMethod.POST)
	public String updqteCuishou_repayno(HttpSession session,HttpServletRequest request,JCollection jCollection){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
//			jCollection.setAdminName(new String(jCollection.getAdminName().getBytes("ISO-8859-1"), "UTF-8"));
//			jCollection.setRemark(new String(jCollection.getRemark().getBytes("ISO-8859-1"), "UTF-8"));
			int i =accountService.updqteCuishou_repayno(jCollection);
			request.setAttribute("jCollection",jCollection);
		}catch(Exception e){
			e.printStackTrace();
		}
		return  "redirect:collection.html";
	}
	//催收管理
	@RequestMapping(value="collection.html",method=RequestMethod.GET)
	public String toEstimate(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CollectionPager pagerModel){
		System.out.println("还款状态"+pagerModel.getRep());
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
					return "login";
			}
			pagerModel=(CollectionPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CollectionPager userBankPager1=accountService.selectCollectionPager(pagerModel);
			List<JCollection> list = userBankPager1.getList();
			System.out.println("催收订单的所有订单"+list);
			for(int i=0;i<list.size();i++){
				LmRepay lmRepay = repayService.selectByPrimaryKey1(list.get(i).getRepayNo());
				accountService.updateRepayStatusByRepayNo(lmRepay.getRepayStatus(), list.get(i).getRepayNo());
			}
			CollectionPager userBankPager=accountService.selectCollectionPager(pagerModel);
			setIfEndty(pagerModel);
			System.out.println("^^^^^"+userBankPager);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("催收管理");
			menuTitle.setMenu("催收订单");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "collection";
	}
	
	//催收统计
	@RequestMapping(value = "collectionStat.html",method = RequestMethod.GET)
	public String toCollectionStat(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CollectionPager pagerModel){
		try{
			SysUser sysUser = (SysUser)session.getAttribute(memberAdmin);
			if(sysUser == null){
				return "login";
			}
			pagerModel=(CollectionPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CollectionPager userBankPager=accountService.selectCollectionPager(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("催收管理");
			menuTitle.setMenu("催收统计");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "collectionStat";
	}
	
	//奖励明细
	@RequestMapping(value = "rewardList.html",method = RequestMethod.GET)
	public String toRewardList(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,RewardPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(RewardPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			RewardPager userBankPager=accountService.selectRewardPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("推广管理");
			menuTitle.setMenu("奖励明细");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "rewardList";
	}
	
	//用户推广记录
	@RequestMapping(value="tuiguang.html",method=RequestMethod.GET)
	public String toRewardList(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,UserAccountPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(UserAccountPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			UserAccountPager userAccountPager=accountService.selectTuiGuangPage(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userAccountPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("推广管理");
			menuTitle.setMenu("推广用户");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "tuiguangList";
	}
	
	
}
